﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Test
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        private static readonly string KeyboardFilePath = string.Concat(AppDomain.CurrentDomain.BaseDirectory, "InputKeyboard.exe");
        private static string? ShowingType;
        private static string? ShowingTitle;
        private static bool MultiTouch = false;
        /// <summary>
        /// 显示软键盘
        /// </summary>
        /// <param name="type">键盘布局</param>
        /// <param name="title">标题</param>
        public static void ShowKeyboard(string type = "normal", string? title = null)
        {
            if (string.IsNullOrEmpty(type)) type = "normal";
            if (string.IsNullOrEmpty(title)) title = "";
            string arguments = string.Concat("layout=", type, " opacity=0.85 multitouch=", MultiTouch, string.IsNullOrEmpty(title) ? "" : string.Concat(" title=", title));
            if (type.Equals(ShowingType) && title.Equals(ShowingTitle))
            {
                try
                {
                    var current = Process.GetCurrentProcess();
                    var ps = Process.GetProcessesByName("InputKeyboard");
                    if (ps.Length > 0) return;
                    Process.Start(new ProcessStartInfo(KeyboardFilePath, arguments));
                }
                catch (Exception)
                {
                    Process.Start(new ProcessStartInfo(KeyboardFilePath, arguments));
                }
            }
            else
            {
                Process.Start(new ProcessStartInfo(KeyboardFilePath, arguments));
                ShowingType = type;
            }
        }

        private void TextBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ShowKeyboard("number");
        }

        private void PasswordBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ShowKeyboard("letters");
        }
    }
}
